package work.shyk;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * description: <br>
 * createTime: 2021/5/19 10:15 <br>
 *
 * @author zzh
 */
public class Test {
    public static void main(String[] args) throws Exception {
        //String cmd = "cmd /c cd d:&&dir";
        //String cmd = "cmd /c cd D:\\IDEA\\license-cloud\\data/1/&&go build xjar.go";
        String cmd = "/bin/sh -c cd /usr/local&&ls";
        StringTokenizer st = new StringTokenizer(cmd);
        String[] cmdarray = new String[st.countTokens()];
        for (int i = 0; st.hasMoreTokens(); i++)
            cmdarray[i] = st.nextToken();

        System.out.println(Arrays.deepToString(cmdarray));
        System.out.println(Arrays.deepToString(cmdarray));

        Runtime rt = Runtime.getRuntime();
        Process p = rt.exec(cmdarray);
        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));

        while(true){
            String line = br.readLine();
            if (line == null) {
                break;
            }
        }
        br.close();
        try {
            //等待Process结束
            p.waitFor();
        } catch (InterruptedException e) {
            System.out.println("p.waitFor()出现错误");
        }
        //返回执行结果值
        System.out.println(p.exitValue());
    }
}
